package com.itheima.controller;

import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.service.AdminTagService;
import com.itheima.vo.TagVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/tag")
@Api(value = "后台管理模块",description = "博客标签相关接口")
public class AdminTagController {

    @Autowired
    private AdminTagService adminTagService;

    /**
     * 增加标签
     * @param tagVo
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody TagVo tagVo){
        try {
            adminTagService.add(tagVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error","添加失败");
        }

        return new Result("success","添加成功");
    }

    /**
     * 批量删除标签
     * @param tagVoList
     * @return
     */
    @PostMapping("/deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestBody List<TagVo> tagVoList){
        try {
            adminTagService.deleteBatch(tagVoList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error","批量删除失败");
        }

        return new Result("success","批量删除成功");
    }

    /**
     * 编辑标签
     * @param tagVo
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public Result edit(@RequestBody TagVo tagVo){
        try {
            adminTagService.edit(tagVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error","编辑失败");
        }

        return new Result("success","编辑成功");
    }

    /**
     * 获取标签列表 根据条件进行分页查询
     * @param tagVo
     * @return
     */
    @PostMapping("/getList")
    @ResponseBody
    public Result getList(@RequestBody TagVo tagVo ){
        PageResult pageResult = adminTagService.findAll(tagVo.getKeyword(),tagVo.getCurrentPage(),tagVo.getPageSize());
        return new Result("success",pageResult);
    }


    /**
     * 置顶标签
     * @param tagVo
     * @return
     */
    @PostMapping("/stick")
    @ResponseBody
    public Result stickTag(@RequestBody TagVo tagVo){
        try {
            adminTagService.stickTag(tagVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error","置顶失败");
        }

        return new Result("success","置顶成功");
    }



    /**
     * 通过引用量排序标签
     * @return
     */
    @PostMapping("/tagSortByCite")
    @ResponseBody
    public Result getTagSortByCite(){
        try {
            adminTagService.getTagSortByCite();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error","排序失败");
        }
        return new Result("success", "排序成功");
    }


    /**
     * 通过点击量排序标签
     * @return
     */
    @PostMapping("/tagSortByClickCount")
    @ResponseBody
    public Result getTagSortByClickCount(){
        try {
            adminTagService.getTagSortByClickCount();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error","排序失败");
        }
        return new Result("success", "排序成功");
    }
}
